/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cookie;

import com.github.junrar.extract.ExtractArchive;
import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author wimvandecasteele
 */
public class FXMLRootViewController implements Initializable {
    
    private final Log fLogger = LogFactory.getLog(FXMLRootViewController.class);
    private boolean readyToProcess = true;

    @FXML
    private Button btnProcess;
    @FXML
    private CheckBox isVideos;
    @FXML
    private CheckBox isSubtitles;
    @FXML
    private Text txtInfo;
    @FXML
    private ScrollPane scrollPane;    
    @FXML
    private TextField locationDir;
    @FXML
    private TextField destinationDir;
    
    
    @FXML protected void processAction(ActionEvent event) {
        readyToProcess = false;
        Date currentDate = new Date();
        setInfoText("Starting processing at "+currentDate.toString());
        File folder = new File(locationDir.getText());
        proccessFiles(folder, destinationDir.getText());
        setInfoText("Processing completed at "+currentDate.toString());
        
    }
    
    private void setInfoText(String pLine)
    {
        String previousText = txtInfo.getText();
        txtInfo.setText(previousText + "\n" + pLine);
        scrollPane.setVvalue(scrollPane.getVmax());
    }
    
    public void proccessFiles(final File pFolder,String pFolderDestination) 
    {
        for (final File vFile : pFolder.listFiles()) 
        {
            if (vFile.isDirectory()) {
                fLogger.debug("");
                fLogger.debug("" +vFile.getName());
                proccessFiles(vFile,pFolderDestination);
            } 
            else 
            {
                if(vFile.getName().indexOf(".rar") > -1)
                {
                    fLogger.debug("Will try to unrar "+vFile.getName());
                    unrarFile(vFile, pFolderDestination);
                }
                if(vFile.getName().indexOf(".avi") > -1)
                {
                    fLogger.debug("--- " +vFile.getName());
                    moveFile(vFile,pFolderDestination);
                }
                
            }
        }
    }
    
    public void moveFile(File pFile, String pFolderDestination)
    {
        if(pFile.renameTo(new File(pFolderDestination + pFile.getName())))
        {
            fLogger.debug("File is moved successful!");
            setInfoText("Moved "+ pFile.getName() + " to destination folder");
        }
        else
        {
            fLogger.debug("File is failed to move!");
            setInfoText("Failed to move "+ pFile.getName() + " to destination folder");
        }
    }
    
    public void unrarFile(File pRarFile, String pDestination)
    {
        final File destinationFolder = new File(pDestination);  
        ExtractArchive vExtractArchive = new ExtractArchive();  
        vExtractArchive.extractArchive(pRarFile, destinationFolder);
        fLogger.debug("Unrarred "+ pRarFile.getName());
        setInfoText("Unrarred "+ pRarFile.getName());
    }
    
    public void deleteSamplesFromCopy(final File folder)
    {
        for (final File vFile : folder.listFiles()) 
        {
            if (vFile.isDirectory()) {
                deleteSamplesFromCopy(vFile);
            } 
            else 
            {
                if(vFile.getName().indexOf("sample") > -1)
                {
                   if(vFile.delete())
                    {
                         fLogger.debug(vFile.getName() + " is deleted!");
                    }
                    else
                    {
                         fLogger.debug("Delete operation is failed.");
                    }
                }
            }
        }
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    
    
}
